package com.laxcen.controller2;

import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.config.MyLog;
import com.laxcen.smoke.dto.req.OrderGetOneReq;
import com.laxcen.smoke.dto.req.OrderTabSelectReq;
import com.laxcen.smoke.dto.req.RemoteOpenDoorReq;
import com.laxcen.smoke.dto.req.RoleFlagPageReq;
import com.laxcen.smoke.dto.res.OrderAllListRes;
import com.laxcen.smoke.dto.res.OrderListRes;
import com.laxcen.smoke.service.OrderService;
import com.laxcen.smoke.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "获取订单管理列表",notes =
            "请求信息： \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 用户权限 (0超级管理员 1普通管理员) \n" +
                    "&emsp; province &nbsp; : &nbsp; 省(超级管理员不传此字段) \n" +
                    "&emsp; city &nbsp; : &nbsp; 市 (超级管理员不传此字段)\n" +
                    "&emsp; area &nbsp; : &nbsp; 区 (超级管理员不传此字段)\n" +
            "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
            "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; opid &nbsp; : &nbsp; 操作用户id \n" +
                    "&emsp; smartno &nbsp; : &nbsp; 智能柜编号 \n" +
                    "&emsp; boxno &nbsp; : &nbsp; 格子编号 \n" +
                    "&emsp; optime &nbsp; : &nbsp; 操作时间 \n" +
                    "&emsp; optype &nbsp; : &nbsp; 操作类型 0存入 1取出 \n" +
                    "&emsp; orderno &nbsp; : &nbsp; 关联订单 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/getalllist")
    @RequiresPermissions("order")
    public Response<PageInfo<OrderAllListRes>> getAllList(@RequestBody RoleFlagPageReq roleFlagPageReq){
        if (roleFlagPageReq.getPageNo()==null){
            roleFlagPageReq.setPageNo(1);
        }
        if (roleFlagPageReq.getPageSize()==null){
            roleFlagPageReq.setPageSize(10);
        }
        if (roleFlagPageReq.getPageNo()==null&&roleFlagPageReq.getPageSize()==null){
            roleFlagPageReq.setPageNo(1);
            roleFlagPageReq.setPageSize(10);
        }
        return orderService.getOrderList(roleFlagPageReq.getPageNo(),roleFlagPageReq.getPageSize(),roleFlagPageReq.getRoleflag(),roleFlagPageReq.getProvince(),roleFlagPageReq.getCity(),roleFlagPageReq.getArea());
    }


    @ApiOperation(value = "订单管理详情页",notes =
            "请求信息： \n" +
                    "&emsp; orderno &nbsp; : &nbsp; 订单号 \n" +

            "返回信息： \n" +
                    "&emsp; orderInRes &nbsp; : &nbsp; 存入的订单详情信息 \n" +
                    "&emsp; orderOutRes &nbsp; : &nbsp; 取出的订单详情信息 \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; opid &nbsp; : &nbsp; 操作人id \n" +
                    "&emsp; smartno &nbsp; : &nbsp; 智能柜编号 \n" +
                    "&emsp; boxno &nbsp; : &nbsp; 格子编号 \n" +
                    "&emsp; optime &nbsp; : &nbsp; 操作时间 \n" +
                    "&emsp; optype &nbsp; : &nbsp; 操作类型 0存入 1取出 \n" +
                    "&emsp; orderno &nbsp; : &nbsp; 关联订单 \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; smartno &nbsp; : &nbsp; 智能柜编号 \n" +
                    "&emsp; boxno &nbsp; : &nbsp; 格子编号 \n" +
                    "&emsp; opresult &nbsp; : &nbsp; 操作结果 0在柜 1取出 \n" +
                    "&emsp; opouttime &nbsp; : &nbsp; 取出时间 \n" +
                    "&emsp; remark &nbsp; : &nbsp; 备注 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/getdetaillist")
    @RequiresPermissions("order:getone")
    public Response<OrderListRes> getDetailAllList(@RequestBody OrderGetOneReq orderGetOneReq){
        return orderService.getOrderDeatilList(orderGetOneReq.getOrderno());
    }

//    @ApiOperation(value = "订单管理详情页点击处理按钮",notes =
//            "请求信息： \n" +
//                    "&emsp; id &nbsp; : &nbsp; id \n" +
//                    "返回码： \n" +
//                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
//                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
//    @PostMapping(value = "/update")
//    public Response update(@RequestBody OrderUpdateReq orderUpdateReq){
//        return orderService.update(orderUpdateReq.getId(),orderUpdateReq.getOpresult(),orderUpdateReq.getRemark());
//    }

    @ApiOperation(value = "选定指定设备和格子远程开门操作",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; username &nbsp; : &nbsp; 当前登录用户的用户名 \n" +
                    "&emsp; smartno &nbsp; : &nbsp; 智能柜编号 \n" +
                    "&emsp; boxno &nbsp; : &nbsp; 格子编号(几号格子) \n" +
                    "&emsp; orderno &nbsp; : &nbsp; 订单号 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 600 &nbsp; : &nbsp; 开门错误信息  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/opendoor")
    @MyLog(operation = "远程开门")
    @RequiresPermissions("order:open")
    public Response opendoor(@RequestBody RemoteOpenDoorReq remoteOpenDoorReq ){
        return orderService.closeDoor(remoteOpenDoorReq.getId(),remoteOpenDoorReq.getUsername(),remoteOpenDoorReq.getSmartno(),remoteOpenDoorReq.getBoxno(),remoteOpenDoorReq.getOrderno());
    }

    @ApiOperation(value = "订单管理导航栏搜索",notes =
            "请求信息： \n" +
                    "&emsp; searchtext &nbsp; : &nbsp; 搜索内容(订单号、姓名、智能柜编号) \n" +
                    "&emsp; starttimme &nbsp; : &nbsp; 时间左区间 \n" +
                    "&emsp; endtime &nbsp; : &nbsp; 时间右区间 \n" +
                    "&emsp; roleflag &nbsp; : &nbsp; 用户权限 (0超级管理员 1普通管理员) \n" +
                    "&emsp; province &nbsp; : &nbsp; 省(超级管理员不传此字段) \n" +
                    "&emsp; city &nbsp; : &nbsp; 市 (超级管理员不传此字段)\n" +
                    "&emsp; area &nbsp; : &nbsp; 区 (超级管理员不传此字段)\n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
            "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; opid &nbsp; : &nbsp; 下单用户id \n" +
                    "&emsp; opname &nbsp; : &nbsp; 下单用户姓名 \n" +
                    "&emsp; smartno &nbsp; : &nbsp; 智能柜编号 \n" +
                    "&emsp; boxno &nbsp; : &nbsp; 格子编号 \n" +
                    "&emsp; optime &nbsp; : &nbsp; 操作时间 \n" +
                    "&emsp; optype &nbsp; : &nbsp; 操作类型 0存入 1取出 \n" +
                    "&emsp; orderno &nbsp; : &nbsp; 关联订单 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/gettablist")
    @RequiresPermissions("order:get")
    public Response<PageInfo<OrderAllListRes>> getTabList(@RequestBody OrderTabSelectReq orderTabSelectReq) throws ParseException {
        if (orderTabSelectReq.getPageNo()==null){
            orderTabSelectReq.setPageNo(1);
        }
        if (orderTabSelectReq.getPageSize()==null){
            orderTabSelectReq.setPageSize(10);
        }
        if (orderTabSelectReq.getPageNo()==null&&orderTabSelectReq.getPageSize()==null){
            orderTabSelectReq.setPageNo(1);
            orderTabSelectReq.setPageSize(10);
        }
        return orderService.getTabList(orderTabSelectReq.getSearchtext(),orderTabSelectReq.getStarttime(),orderTabSelectReq.getEndtime(),orderTabSelectReq.getRoleflag(),orderTabSelectReq.getProvince(),orderTabSelectReq.getCity(),orderTabSelectReq.getArea(),orderTabSelectReq.getPageNo(),orderTabSelectReq.getPageSize());
    }

}
